package controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import po.Order;
import service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderController{
	@Autowired
	private OrderService orderService;
	
	@RequestMapping("/select/{page}")
	public String select(Model model, @PathVariable int page) {
		int pageNumber = orderService.count();
		page--;
		if (page >= pageNumber) {
			page = 0;
		}
		page *= 5;
		
		List<Order> list = orderService.orderSelectAllOrder(page);
		ArrayList<Integer> pages = new ArrayList<>();
		for(int i = 1;i <= pageNumber;i++) {
			pages.add(i);
		}
		
		model.addAttribute("orderList",list);
		
		Order order = new Order();
		model.addAttribute("order",order);
		model.addAttribute("pageNumber", pageNumber);
		model.addAttribute("pages", pages);
		return "orderList";
	}
	
	@RequestMapping("/delete")
	public String delete(Model model,int id) {
		int success2 = orderService.orderDelOrderDetail(id);
		int success1 = orderService.orderDelOrderBase(id);
		if(success1 == 0 || success2 == 0) {
			return "delError";
		}
		
		return "forward:/order/select/1";	
	}
	
	@RequestMapping("/selectEml")
	public String selectByEmail(@ModelAttribute Order order, Model model) {
		List<Order> list = orderService.serciveSelectEml(order);
		if (list.size() == 0) {
			model.addAttribute("selectMsg", "查询不到该email");
			return "forward:/order/select/1";
		}
		model.addAttribute("orderList", list);
		model.addAttribute("order", order);
		return "orderList";
	}
}
